//
//  MViewController.swift
//  AreaChainblock
//
//  Created by 赵佟越 on 2018/9/21.
//  Copyright © 2018年 zhaotongyue. All rights reserved.
//

import UIKit

class MViewController: BaseViewController ,UITableViewDelegate, UITableViewDataSource{
    var mytableView : UITableView!
    var nameLaeblStr : NSString!
    override func viewDidLoad() {
        super.viewDidLoad()
        layoutNav()
        self.view.addSubview(myTableView)
    }
    //MARK - Private Methods
    func layoutNav(){
        self.title = "CD";
    }
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    //MARK: - tableView Delegate And DataSource
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
        }
        cell?.backgroundColor = UIColor.clear
        return cell!
    }
    //footer height
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    // row height
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    // header height
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 500
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return nil
    }
    
    // row num
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    // 组数
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    //点击事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let backView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 500));
        
        let backViewTop = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 230))
        backViewTop.backgroundColor = UIColor.clear
        
//        backViewTop.layer.masksToBounds = false;
//        backViewTop.layer.cornerRadius = 3
//        backViewTop.layer.contentsScale = UIScreen.main.scale;
//
//        backViewTop.layer.shadowOpacity = 0.3;
//
//        backViewTop.layer.shadowRadius = 2.0;
//
//        backViewTop.layer.shadowOffset = CGSize.init(width: 0, height: 0);
//
//        backViewTop.layer.shadowPath = UIBezierPath.init(roundedRect: backViewTop.bounds, cornerRadius: 5).cgPath;
        
        backViewTop.layer.shouldRasterize = true;
        
        backViewTop.layer.rasterizationScale = UIScreen.main.scale;
        
        
        
        let topphoto = UIImageView()
        topphoto.layer.cornerRadius = 23;
        backViewTop.addSubview(topphoto)
        topphoto.snp.makeConstraints { (make) in
            make.centerX.equalTo(backViewTop)
            make.size.equalTo(CGSize(width: 64, height: 64))
            make.top.equalTo(26)
        }
        topphoto.sd_setImage(with: NSURL.init(string: "")! as URL, placeholderImage: UIImage.init(named: "组 1@2x"), options: [], progress: nil, completed:nil)
        
        
        let nameLaebl = UILabel()
        nameLaebl.textAlignment = .center
        nameLaebl.text = String(format: "%.3f", Float(nameLaeblStr as String!)!);
        backViewTop.addSubview(nameLaebl);
        nameLaebl.snp.makeConstraints { (make) in
            make.top.equalTo(topphoto.snp.bottom).offset(15)
            make.size.equalTo(CGSize(width: 200, height: 20))
            make.centerX.equalTo(backViewTop)
        }
        nameLaebl.textAlignment = .center
        nameLaebl.font = UIFont.systemFont(ofSize: 15)
//        nameLaebl.text = "2208"
        nameLaebl.font = UIFont.boldSystemFont(ofSize: 20)
        nameLaebl.textColor = UIColor("#307BF6")
        
        let phoneLabel = UILabel()
        phoneLabel.textAlignment = .center
        backViewTop.addSubview(phoneLabel);
        phoneLabel.snp.makeConstraints { (make) in
            make.top.equalTo(nameLaebl.snp.bottom).offset(15)
            make.size.equalTo(CGSize(width: 200, height: 16))
            make.centerX.equalTo(backViewTop)
        }
        phoneLabel.textColor = UIColor("909090")
        phoneLabel.textAlignment = .center
        phoneLabel.font = UIFont.systemFont(ofSize: 15)
        phoneLabel.text = ""
        
        backView .addSubview(backViewTop)
        
        
        let testLabel = LablePadding(paddingTop : 5, paddingLeft : 10 , paddingBottom : 0 , paddingRight : 10 )
        testLabel.backgroundColor = UIColor.clear
        testLabel.text = XALanguageManager.getString(key: "CDDetail")
        testLabel.numberOfLines = 0
        testLabel.font = UIFont.systemFont(ofSize: 14)
        testLabel.textAlignment = .left
        testLabel.textColor = UIColor.white
        testLabel.setText(text: testLabel.text, lineSpaing: 8, labelWidth: kScreenWidth - 15*2, font: UIFont.systemFont(ofSize: 14)) //设置行间距

        let topView01 = UIView(frame: CGRect(x: 15, y: 190, width: kScreenWidth - 30, height: 80))
        topView01.backgroundColor = UIColor.clear
        
//        topView01.layer.masksToBounds = false;
//        topView01.layer.cornerRadius = 3
//        topView01.layer.contentsScale = UIScreen.main.scale;
//
//        topView01.layer.shadowOpacity = 0.3;
//
//        topView01.layer.shadowRadius = 2.0;
//
//        topView01.layer.shadowOffset = CGSize.init(width: 0, height: 0);
//
//        topView01.layer.shadowPath = UIBezierPath.init(roundedRect: topView01.bounds, cornerRadius: 5).cgPath;
//
//        topView01.layer.shouldRasterize = true;
//
//        topView01.layer.rasterizationScale = UIScreen.main.scale;
        
        
        backView.addSubview(topView01)
        
        
        
        
        
        let TestLabelH = testLabel.text?.heightWithLabelFont(font: UIFont.systemFont(ofSize: 14), labelWidth: kScreenWidth - 15*2 - 20, lineSpacing: 8, label: testLabel)
        
        testLabel.frame = CGRect(x: 15, y: 260 + 40, width: kScreenWidth - 15*2, height: TestLabelH! + 10)
        testLabel.isHidden = TestLabelH == 0
        testLabel.backgroundColor = UIColor.clear
        
        
//        testLabel.layer.masksToBounds = false;
//        testLabel.layer.cornerRadius = 3
//        testLabel.layer.contentsScale = UIScreen.main.scale;
//
//        testLabel.layer.shadowOpacity = 0.3;
//
//        testLabel.layer.shadowRadius = 2.0;
//
//        testLabel.layer.shadowOffset = CGSize.init(width: 0, height: 0);
//
//        testLabel.layer.shadowPath = UIBezierPath.init(roundedRect: testLabel.bounds, cornerRadius: 5).cgPath;
//
//        testLabel.layer.shouldRasterize = true;
//
//        testLabel.layer.rasterizationScale = UIScreen.main.scale;
        backView.addSubview(testLabel)
        
        
        let bottomBtn = UIButton()
        bottomBtn.backgroundColor = UIColor("#E4EDFB")
        topView01.addSubview(bottomBtn)
        bottomBtn.snp.makeConstraints { (make) in
            make.top.equalTo(topView01).offset(10)
            make.size.equalTo(CGSize(width: 40, height: 40))
            make.left.equalTo(topView01).offset(70)
        }
        bottomBtn.setImage(UIImage.init(named: "111组 2@2x"), for: .normal)
        bottomBtn.layer.cornerRadius = 20
        bottomBtn.addTarget(self, action: #selector(MViewController.Recharge), for: .touchUpInside)
        
        let bottomLabel = UILabel()
        topView01.addSubview(bottomLabel)
        bottomLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(topView01).offset(-5)
            make.size.equalTo(CGSize(width: 100, height: 20))
            make.centerX.equalTo(bottomBtn)
        }
        bottomLabel.text = XALanguageManager.getString(key: "Recharge")
        bottomLabel.textColor = UIColor.white
        bottomLabel.textAlignment = .center
        bottomLabel.font = UIFont.systemFont(ofSize: 16)

        
        let bottomBtn1 = UIButton()
        bottomBtn1.backgroundColor = UIColor("#E4EDFB")
        topView01.addSubview(bottomBtn1)
        bottomBtn1.snp.makeConstraints { (make) in
            make.top.equalTo(topView01).offset(10)
            make.size.equalTo(CGSize(width: 40, height: 40))
            make.right.equalTo(topView01).offset(-70)
        }
        bottomBtn1.setImage(UIImage.init(named: "组 2@2x 2"), for: .normal)
        bottomBtn1.layer.cornerRadius = 20
        bottomBtn1.setTitle(XALanguageManager.getString(key: "Recording"), for: .normal)
        
        bottomBtn1.addTarget(self, action: #selector(MViewController.recordingAction), for: .touchUpInside)
        
        
        let bottomLabel1 = UILabel()
        topView01.addSubview(bottomLabel1)
        bottomLabel1.snp.makeConstraints { (make) in
            make.bottom.equalTo(topView01).offset(-5)
            make.size.equalTo(CGSize(width: 100, height: 20))
            make.centerX.equalTo(bottomBtn1)
        }
        
        bottomLabel1.text = XALanguageManager.getString(key: "Recording")
        bottomLabel1.textAlignment = .center
        bottomLabel1.font = UIFont.systemFont(ofSize: 16)
        bottomLabel1.textColor = UIColor.white
        
        
        let testLabel1 = LablePadding(paddingTop : 5, paddingLeft : 10 , paddingBottom : 0 , paddingRight : 10 )
        testLabel1.backgroundColor = UIColor.clear
        testLabel1.text = XALanguageManager.getString(key: "兑换规则\n1. 兑换数额不得少于1000CD；\n2. CD不可交易；")
        testLabel1.numberOfLines = 0
        testLabel1.textColor = UIColor.white
        testLabel1.font = UIFont.systemFont(ofSize: 14)
        testLabel1.textAlignment = .left
        
        testLabel1.setText(text: testLabel1.text, lineSpaing: 8, labelWidth: kScreenWidth - 15*2, font: UIFont.systemFont(ofSize: 14)) //设置行间距
        
        let TestLabelH1 = testLabel1.text?.heightWithLabelFont(font: UIFont.systemFont(ofSize: 14), labelWidth: kScreenWidth - 15*2 - 20, lineSpacing: 8, label: testLabel)
        
        testLabel1.frame = CGRect(x: 15, y: 260 + 40 + TestLabelH! + 10 + 10, width: kScreenWidth - 15*2, height: TestLabelH1! + 10)
        testLabel1.isHidden = TestLabelH == 0
        backView.addSubview(testLabel1)
        
        
        return backView
    }
    @objc func recordingAction() {
        //recording
        let RecVC = RecordingListViewController()
        RecVC.isFrom = "M"
        super.navigationController?.pushViewController(RecVC, animated: true)
    }
    @objc func Recharge() {
        self.navigationController?.pushViewController(RechargeListViewController(), animated: true)
    }
    
    
    override func willMove(toParentViewController parent: UIViewController?) {
        
    }
    
    // MARK: - 懒加载
    private lazy var myTableView:UITableView = {
            var tempheight = CGFloat()
            tempheight = 64
            if #available(iOS 11.0.0, *) {
                tempheight = 0
                print("iOS 9.3.5 is available")
            }
            
        let myTableView = UITableView(frame: CGRect(x: 0, y: tempheight, width: kScreenWidth, height: kScreenHeight - tempheight), style: UITableViewStyle.grouped)
        myTableView.delegate = self
        myTableView.dataSource = self
        myTableView.backgroundColor = UIColor.clear
        myTableView.separatorStyle = .none
        myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        return myTableView
    }()
    
}



